package com.vcredit.creditcat.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.model.Merchant;
import com.vcredit.creditcat.persistence.base.MerchantMapper;

/**
 * 
 **/
@Service
public class MerchantService {
	@Autowired
	private MerchantMapper mapper;

	public void insert(Merchant obj) {
		mapper.insert(obj);
	}

	@CacheEvict(value = "merchant")
	public void update(Merchant obj) {
		mapper.update(obj);
	}

	@Cacheable(key = "'Merchant'", value = "merchant")
	public Map<Long, Merchant> findAll() {
		Map<Long, Merchant> merchants = new HashMap<Long, Merchant>();
		for (Merchant merchant : mapper.findAll()) {
			merchants.put(merchant.getMerchantId(), merchant);
		}
		return merchants;
	}

	@SuppressWarnings("unused")
	private List<Merchant> find(Merchant obj) {
		return mapper.find(obj);
	}

	public Merchant findById(Long id) {
		return this.findAll().get(id);
	}
}